﻿using System;
using System.Collections.Generic;
using System.Text;
using VIP.Data.Entities;
using VIP.Domain.DTO;
using VIP.Events;
using VIP.Events.Order;

namespace VIP.Domain.Core.DomainObject
{
    public class OrderObj : AggregateRoot
    {

        public override Guid Id
        {
            get
            {
                if (Order == null)
                {
                    return Guid.Empty;
                }
                return Order.Id;
            }
        }

        /// <summary>
        /// 订单信息
        /// </summary>
        public Order Order { get; set; }

        /// <summary>
        /// 订单明细
        /// </summary>
        public OrderItemValue Items { get; set; }
        /// <summary>
        /// 物流信息
        /// </summary>
        public Logistic Logistics { get; set; }

        internal void Create(CreateOrderDTO dto)
        {
            var mid = Guid.NewGuid();
            //todo要做mapper自动
            Order = new Order()
            {
                Id = mid,
                Amount = 0,
                Code = dto.code,
                CreateTime = DateTime.Now,
                CustomerName = dto.customerName,
                Status = true
            };
            Items = new OrderItemValue();
            foreach (var item in dto.Items)
            {
                Items.OrderItemEntities.Add(
                     new OrderItem()
                     {
                         Id = Guid.NewGuid(),
                         Count = item.count,
                         MId = mid,
                         OrderIndex = item.orderIndex,
                         Price = item.price,
                         ProductCode = item.productCode
                     });
            }
            this.AddEvent(new OrderCreated()
            {
                Order = Order,
                Items = Items.OrderItemEntities
            });
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dto"></param>
        public void AddItem(Guid orderId, OrderItemDTO dto)
        {
            OrderItem obj = new OrderItem() ;//通过dto转换
            this.Items.OrderItemEntities.Add(new OrderItem());
            this.AddEvent(new OrderItemAdded() {
                Order= orderId,
                 Item= obj
            });
        }
    }
}
